// 读取本地excel文件，读取Excel文件对象
function readWorkbookFromLocalFile(file, callback) {
  var reader = new FileReader();
  reader.onload = function (e) {
    var data = e.target.result;
    var workbook = XLSX.read(data, {
      type: 'binary'
    });
    // console.log(workbook);
    if (callback) callback(workbook);
  };
  reader.readAsBinaryString(file);
}

// 转成cvs 要注意Execel表格内容不能包含英文的,不然解析出来的数据格式会有问题
function readWorkbook(workbook) {
  var sheetNames = workbook.SheetNames; // 工作表名称集合
  var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
  var csv = XLSX.utils.sheet_to_json(worksheet, {
    header: 1,
    defval: ''
  });

  let tableArry = [];

  //三星级二维数组暂存
  let ThreeStarsArry = [];

  // 二星级存储数据
  let twoStarArry = [];

  // 一星级存储数据
  let OneStarArry = [];

  // 没有通过数据存储
  let NotPass = [];

  // 错误数据
  let errorArry = [];

  // ES6 写法
  csv.forEach(v => {
    let tdArry = [];
    v.forEach(item => {
      item = item.replace(/\s*/g, ""); //预先处理字符串多余的空格
      item = item.replace(/[\r\n]/g, ""); //去掉回车换行

      tdArry.push(item)
    });
    tdArry[21] = currentLevel(tdArry);
    if (tdArry[21] == "三星级") {
      ThreeStarsArry.push(tdArry)
    } else if (tdArry[21] == "二星级") {
      twoStarArry.push(tdArry)
    } else if (tdArry[21] == "一星级") {
      OneStarArry.push(tdArry)
    } else {
      // 不通过条件
      if ((tdArry[0] == "" || tdArry[1] == "" || tdArry[2] == "" || tdArry[3] == "" || tdArry[4] == "") ||
        // 去除前5个为空的无效数据
        (tdArry[1] == "分公司" && tdArry[2] == "店铺名称" && tdArry[3] == "客户编码" && tdArry[4] == "档位")
      ) {
        errorArry.push(tdArry)
      } else {
        NotPass.push(tdArry)
      }
    }

    tableArry.push(tdArry)
  });

  WriteGradeStars(ThreeStarsArry, 3); //跳转到三星等级 写入html

  WriteGradeStars(twoStarArry, 2); //跳转到二星等级 写入html
  WriteGradeStars(OneStarArry, 1); //跳转到一星等级 写入html
  WriteGradeStars(NotPass, 0) //没有通过
  WriteGradeStars(errorArry, -1) //无效数据
}

// 等级判断
function currentLevel(tdArry) {
  if (tdArry[6] !== "是") {
    return "规范经营违规";
  } else if (tdArry[7] != "通过") {
    return "库存核查未通过";
  } else if (tdArry[9] != "通过") {
    return "库存数据未通过";
  } else if (tdArry[11] != "通过") {
    return "存销比数据未通过"
  } else if (Number(tdArry[12]) < 120) {
    return "扫码笔数小于120笔"
  } else if (tdArry[13].replace('%', '') / 100 < 0.7) {
    return "扫码销售占比小于70%,当前百分比：" + tdArry[13].replace('%', '') / 100;
  } else if (Number(tdArry[14]) < 23) {
    return "扫码销售天数小于23天"
  } else if ((Number(tdArry[12]) >= 1500) &&
    ((tdArry[13].replace('%', '')) / 100 >= 0.9) &&
    (Number(tdArry[14]) >= 23) &&
    (Number(tdArry[17]) >= 0.9 && Number(tdArry[17]) <= 1.1) &&
    (Number(tdArry[16].replace('%', '')) / 100 >= 0.5) &&
    (Number(tdArry[18]) >= 100 || Number(tdArry[19]) >= 20) &&
    (Number(tdArry[20]) >= 0.8 ) 

  ) {
    return "三星级";
  } else if (
    (Number(tdArry[12]) >= 360) &&
    ((tdArry[13].replace('%', '')) / 100 >= 0.8) &&
    (Number(tdArry[14]) >= 23) &&
    (Number(tdArry[18]) >= 20 || Number(tdArry[19]) >= 10) &&
    (Number(tdArry[16].replace('%', '')) / 100 != 0) &&
    (Number(tdArry[17]) >= 0.9 && Number(tdArry[17]) <= 1.1) &&
    (Number(tdArry[20]) >= 0.8 ) 
  ) {
    return "二星级";
  } else if (
    (Number(tdArry[12]) >= 120) &&
    ((tdArry[13].replace('%', '')) / 100 >= 0.7) &&
    (Number(tdArry[14]) >= 23)
  ) {
    return "一星级";
  }
}

//写入三星级
function WriteGradeStars(arr, grade) {

  var str = '';
  for (var i = 0; i <= arr.length - 1; i++) {
    str += '<tr>';
    str += `<td>${i + 1}</td>`;
    for (var j = 1; j <= arr[i].length - 1; j++) {
      str += `<td>${arr[i][j]}</td>`;
    }
    str += '</tr>';
  }
  // 将定义好的内容,写入到taody标签中
  if (grade == 3) {

    document.getElementById("table3").innerHTML = str;
  }
  if (grade == 2) {

    document.getElementById("table2").innerHTML = str;
  }
  if (grade == 1) {

    document.getElementById("table1").innerHTML = str;
  }
  if (grade == 0) {

    document.getElementById("table0").innerHTML = str;
  }
  if (grade == -1) {

    document.getElementById("errortable").innerHTML = str;
  }
  mdui.mutation()
  mdui.updateTables()
}